<?php

session_start();
$currentUser = isset($_SESSION["user"])?$_SESSION["user"]:null;
$userId= $currentUser?$currentUser["Id"]:null;
/*
$theme = isset($_GET["theme"])?$_GET["theme"]:null;
if(!$theme){
  $theme = $_COOKIE["theme"];
}else{
  setcookie("theme",$theme);
}
if(!$theme) $theme = "default";*/

$pageSize = 6;
$pageIndex = isset($_GET["pageIndex"])?$_GET["pageIndex"]:null;
if($pageIndex<=0) $pageIndex = 1;

$queryTitle = isset($_GET["title"])?$_GET["title"]:null;
$queryAuthor = isset($_GET["author"])?$_GET["author"]:null;
$queryMinTime = isset($_GET["minTime"])?$_GET["minTime"]:null;
$queryMaxTime = isset($_GET["maxTime"])?$_GET["maxTime"]:null;

$where = "";
if($queryTitle) {
    $where = " `Title` like ('%$queryTitle%')";
}

if($queryAuthor){
    if($where) $where .= " AND `AuthorName` like ('%$queryAuthor%')"; 
    else $where .= "`AuthorName` like ('%$queryAuthor%')";   
}

if($queryMinTime){
    if($where) $where .= " AND "; 
    $where .= "createTime>='$queryMinTime'";   
}

if($queryMaxTime){
    if($where) $where .= " AND "; 
    $where .= "createTime<='$queryMaxTime'";   
}

if($where) $where = "WHERE $where";

$startRow = ($pageIndex -1)*$pageSize;

$sql = "select * from `article` $where order by `createTime` desc limit $startRow,$pageSize ";
$conn = new mysqli("localhost","root","","myblog");
$rs = $conn->query($sql);
if($conn->error) die($conn->error);
$rows=[];
while($row = $rs->fetch_assoc()){
    $rows[]=$row;
}
$rs->close();

$totalSql = "select count(Id) as total from `article` $where";
$rs = $conn->query($totalSql);
$total = $rs->fetch_assoc()['total'];

$pageCount = ceil($total/$pageSize);

function makePageUrl($pageIndex){
    $url = "article_list.php?";
    foreach($_GET as $k =>$v){
        if($k==="pageIndex"){
            continue;
        }
        $url .=$k."=".$v."&";
    }
    $url.="pageIndex=".$pageIndex;
    return $url;
}
/*
function makePageUrl($pageNo){
    $currUrl = $_SERVER['REQUEST_URI'];
    return preg_replace('/pageIndex=\d+/',"pageIndex=".$pageNo,$currUrl);
}
*/
?>

<!doctype html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <title>文章列表</title>
        <link type="text/css" rel="stylesheet" href="themes/default1.css">

    </head>
    <body>
    <div id="layout">
            <div id="header">
                <div class="container">
                    <h1 class="col-9">我的博客系统</h1>
                    <div id="user-info" class="col-3"> 
                        <?php if($currentUser){ ?>
                            <span id="user-name"><?=$currentUser["Username"]?></span>
                            <a href="logout.php">退出</a>
                            <?php } else{ ?>     
                                <span id="user-name">访客</span>           
                                <a href="login.html">请登陆</a>       
                        <?php } ?>
                        
                    </div>
                </div>
                <div id="main-menu">
                    <ul class="topMenu">
                        <li>首页</li>
                        <li><a href="article_list.php">文章</a></li>
                        <li id="manage">
                            <span>管理</span>
                            <ul>
                                <li><span>用户</span></li>
                                <li>
                                    <span>基础数据</span>
                                    <ul>
                                        <li>皮肤管理</li>
                                        <li>兴趣管理</li>
                                    </ul>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </div>
            </div>
            <div id="main">
                <div id="workspace">
                <span class="btn" id="listBtn">列表</span>
                <span class="btn" id="cardBtn">卡片</span>
                    <div id="panel" class="list">           
                        <div class="table">
                            <div class="thead">
                                <div class="tr">
                                    <div class="th"><span class="title">标题</span></div>
                                    <div class="th">作者</div>
                                    <div class="th">创建时间</div>
                                    <div class="th">操作</div>
                                </div>
                            </div>
                            <div class="tbody" >
                                <?php foreach($rows as $row){?>
                                <div class="tr">
                                    <div class="td Title">
                                    <a href="article_detail.php?Id=<?=$row["Id"]?>"><?=$row["Title"]?></a>
                                    </div>
                                    <div class="td">
                                        <?=$row["AuthorName"]?>
                                    </div>
                                    <div class="td">
                                        <?=$row["CreateTime"]?><br>
                                        <?=$row["UpdateTime"]?>
                                    </div>
                                    <div class="td">
                                        <a href="article_detail.php?Id=<?=$row["Id"]?>">详情</a>
                                        <?php if($userId===$row["AuthorId"]) { ?>
                                            <a href="article_modify.php?Id=<?=$row["Id"]?>">修改</a>
                                            删除 
                                        <?php } ?>
                                    </div>
                                </div>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                    <div id="recordInfo"> 
                        共<?=$total?>条记录，<?=$pageCount?>页，当前第<input type="text" value="<?=$pageIndex?>" onblur="jumpTo(this.value)"/>页，
                        <a href="article_list.php?<?=makePageUrl(1)?>">首页</a>< 
                            <?php for($i=1 ; $i<=$pageCount ; $i++){?>
                                <a href="article_list.php?<?=makePageUrl($i)?>"> <?=$i?> </a>                            
                            <?php } ?>
                        > 
                        <a href="article_list.php?<?=makePageUrl($pageCount)?>">尾页</a>
                    </div>
                </div>
            <script type="text/javascript">
                listBtn.onclick = function(){
                    var span = document.getElementById("panel");
                    span.className = "list";
                };
                cardBtn.onclick = function(){
                    var span = document.getElementById("panel");
                    span.className = "card";
                };     

                //菜单下拉展开
                var mainMenu = document.getElementById("main-menu");
                var lis = mainMenu.getElementsByTagName("li");
                for(var i=0,j=lis.length;i<j;i++){
                    var li = lis[i];
                    li.onmouseenter=function(){
                        var enterLi = this;
                        var ul = enterLi.children[1];
                        if(!ul) return;
                        ul.style.opacity = 0.5;
                        var tick = setInterval(function(){
                            var opacity = parseFloat(ul.style.opacity);//这个函数是把字符串变成数字（浮点数）
                            opacity += 0.07;
                            if(opacity>=1){
                              opacity=1;  
                              clearInterval(tick);
                            } 
                            ul.style.opacity =opacity;
                        },100);
                    };
                }           
            
            </script>


                <div id="extras"></div>
            </div>
            <div id="footer">
                <div id="quick-access"></div>
                <div id="copyright">
                    <p>fan@qq.com All right reserved.</p>
                </div>
            </div>
        </div>
 
    </body>

</html>